package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParam;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("查询全部的学生数据");
        PageResult<Student> studentList=studentService.list(studentQueryParam);
        return Result.success(studentList);
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids){
        List<Integer> list = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .toList();
        log.info("批量删除:{}",list);
        studentService.delete(list);
        return Result.success();

    }
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("新增{}",student);
        studentService.add(student);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询{}",id);
        Student student=studentService.getById(id);
        return Result.success(student);
    }
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改{}",student);
        studentService.update(student);
        return Result.success();
    }
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("根据id查询{}",id);
        studentService.violation(id,score);
        return Result.success();
    }
}
